package com.tjetc.view.teacherview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.Signup;
import com.zhiyun.domain.Student;
import com.zhiyun.service.CourseService;
import com.zhiyun.service.SignupService;
import com.zhiyun.service.StudentService;
import com.zhiyun.vo.Result;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class MyCourse_Check {
    StudentService studentService= CilentProxy.getProxyInstance(StudentService.class);
    CourseService courseService=CilentProxy.getProxyInstance(CourseService.class);
    SignupService signupService= CilentProxy.getProxyInstance(SignupService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course1){
        while (true){
            System.out.println("~~~~~~~~~~签到~~~~~~~~~~");
            System.out.println("1. 签到列表");
            System.out.println("2. 查看签到信息");
            System.out.println("3. 发布签到");
            System.out.println("0. 返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allCheck(course1);
                    break;
                case 2:
                    selectCheckById(course1);
                    break;
                case 3:
                    inseretCheck(course1);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
            }
        }
    }

    private void allCheck(Course course1) {
        while (true){
            Result result = signupService.SignupByCourseId(course1.getId());
            if (result.getCode()==200){
                List<Date> obj = (List<Date>) result.getObj();
                for (Date date : obj) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String format = dateFormat.format(date);
                    System.out.println(format);
                }
                for (Date date : obj) {
                    Result result1 = signupService.selectSignup(date, course1.getId());
                    if (result.getCode()==200){
                        List<Signup> obj1 = (List<Signup>) result1.getObj();
                        for (Signup signup : obj1) {
                            Result result2 = studentService.myInfo(signup.getStudentId());
                            if (result2.getCode()==200){
                                Student obj2 = (Student) result2.getObj();
                                System.out.println("签到id"+signup.getId()+"   学生姓名"+obj2.getTruename()+"     是否签到:"+(signup.getIsSignup()==0?"没有签到":"已签到"));
                            }


                        }
                    }
                }
            }else {
                System.out.println("加载失败");
            }
            break;
        }
    }

    private void selectCheckById(Course course1) {
        while (true){
            HashMap<String, Integer> hashMap = new HashMap<>();
            Result result = signupService.SignupByCourseId(course1.getId());
            if (result.getCode()==200){
                List<Date> obj = (List<Date>) result.getObj();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Integer i1=1;
                for (Date date : obj) {
                    String format = dateFormat.format(date);
                    hashMap.put(format,i1++);
                }
                Iterator<String> iterator1 = hashMap.keySet().iterator();
                while (iterator1.hasNext()){
                    String next = iterator1.next();
                    System.out.println(hashMap.get(next)+"  "+next);
                }


                System.out.println("请输入想要查看的签到id");
                Integer i = scanner.nextInt();
                boolean a=false;
                String date=null;
                Iterator<String> iterator = hashMap.keySet().iterator();
                while (iterator.hasNext()){
                    String next = iterator.next();
                    if (hashMap.get(next)==i){
                        date=next;
                    }
                }
                if (date!=null){
                    try {
                        Date parse = dateFormat.parse(date);
                        Result result1 = signupService.selectSignup(parse, course1.getId());
                        if (result.getCode()==200){
                            List<Signup> obj1 = (List<Signup>) result1.getObj();
                            for (Signup signup : obj1) {
                                Result result2 = studentService.myInfo(signup.getStudentId());
                                if (result2.getCode()==200){
                                    Student obj2 = (Student) result2.getObj();
                                    System.out.println("签到id"+signup.getId()+"   学生姓名"+obj2.getTruename()+"     是否签到:"+(signup.getIsSignup()==0?"没有签到":"已签到"));
                                }
                            }
                            while (true){
                                System.out.println("是否进行补签?(Y/N)");
                                String next = scanner.next();
                                if (next.equals("Y")){
                                    System.out.println("请输入补签id");
                                    int i2 = scanner.nextInt();
                                    boolean b=false;
                                    for (Signup signup : obj1) {
                                        if (i2==signup.getId()&&signup.getIsSignup()==0){
                                            b=true;
                                            break;
                                        }else {
                                            System.out.println("补签的签到不存在,或者已经签到");
                                        }
                                    }
                                    if (b){
                                        Result result2 = signupService.reSignup(i2);
                                        System.out.println(result2.getMsg());
                                    }
                                }else {
                                    break;
                                }
                            }
                        }else {
                            System.out.println("获取签到信息不存在");
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }else {
                    System.out.println("想要查看的签到信息不存在");
                }
            }else {
                System.out.println("加载失败");
            }
            break;
        }

    }

    /**
     * 需要所有的学生,循环获取
     * @param course
     */

    private void inseretCheck(Course course) {
        while (true){
            Result result = courseService.AllStudent(course.getId());
            List<Student> obj = (List<Student>) result.getObj();
            List<Signup> list=new ArrayList<>();
            for (Student student : obj) {
                System.out.println("~~~");
                list.add(new Signup(null,course.getId(),student.getId(),0,new Date(),null,null));
            }
            System.out.println(list);
            Result insert = signupService.insert(list);
            String obj1 = (String) insert.getObj();
            System.out.println("签到码:  "+obj1);
            System.out.println(insert.getMsg());
            break;
        }
    }
}
